﻿using System.Collections.Generic;
using System.Linq;

namespace Songhay.Mvvm.Extensions
{
    using Songhay.Models;

    /// <summary>
    /// Extensions of <see cref="MenuItemDatum"/>
    /// </summary>
    public static class MenuItemDatumExtensions
    {
        /// <summary>
        /// Returns the Default Selection
        /// <c>IsDefaultSelection == true</c>
        /// or the First <see cref="MenuItemDatum"/>.
        /// </summary>
        /// <param name="data">The data.</param>
        public static MenuItemDatum DefaultOrFirst(this IEnumerable<MenuItemDatum> data)
        {
            if(data == null) return null;

            if(data.Where(i => i.IsDefaultSelection == true).Count() > 0)
            {
                return data.FirstOrDefault(i => i.IsDefaultSelection == true);
            }
            else
            {
                return data.FirstOrDefault();
            }
        }

        /// <summary>
        /// Fluently returns <see cref="MenuItemDatum" /> with child item.
        /// </summary>
        /// <param name="datum">The datum.</param>
        /// <param name="child">The child.</param>
        public static MenuItemDatum WithChildItem(this MenuItemDatum datum, MenuItemDatum child)
        {
            if(datum == null) return null;
            if(child == null) return datum;
            datum.ChildItems = new MenuItemDatum[] { child };
            return datum;
        }

        /// <summary>
        /// Fluently returns <see cref="MenuItemDatum" /> with child items.
        /// </summary>
        /// <param name="datum">The datum.</param>
        /// <param name="list">The list.</param>
        public static MenuItemDatum WithChildItems(this MenuItemDatum datum, MenuItemDatum[] list)
        {
            if(datum == null) return null;
            if(list == null) return datum;
            datum.ChildItems = list;
            return datum;
        }

        /// <summary>
        /// Fluently returns <see cref="MenuItemDatum" /> with child items.
        /// </summary>
        /// <param name="datum">The datum.</param>
        /// <param name="list">The list.</param>
        public static MenuItemDatum WithChildItems(this MenuItemDatum datum, List<MenuItemDatum> list)
        {
            if(datum == null) return null;
            if(list == null) return datum;
            datum.ChildItems = list.ToArray();
            return datum;
        }

        /// <summary>
        /// Fluently returns <see cref="MenuItemDatum" /> without children.
        /// </summary>
        /// <param name="datum">The datum.</param>
        public static MenuItemDatum WithoutChildren(this MenuItemDatum datum)
        {
            if(datum == null) return null;
            return new MenuItemDatum
            {
                Description = datum.Description,
                DisplayText = datum.DisplayText,
                Id = datum.Id,
                ItemCategory = datum.ItemCategory,
                ItemName = datum.ItemName,
                SortOrdinal = datum.SortOrdinal
            };
        }
    }
}
